package com.nmgydy.admin.controller;

import com.alibaba.fastjson.JSON;
import com.nmgydy.beans.ListBean;
import com.nmgydy.beans.ResultBean;
import com.nmgydy.model.Court;
import com.nmgydy.model.Organization;
import com.nmgydy.util.Functions;
import com.nmgydy.util.HibernateDao;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;


@Controller
@RequestMapping("/admin/servant")
public class ServantController {

	@Resource
	private HibernateDao hibernateDao;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/servant/list";
	}

	@RequestMapping(value = "search", method = RequestMethod.GET)
	public String search(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/servant/search";
	}

	@RequestMapping(value = "detail", method = RequestMethod.GET)
	public String detail(Integer id,HttpServletRequest request, HttpServletResponse response, Model model) {
		String hql = "from Servant where id=:id";
		Map<String, Object> params = new HashMap<>();
		params.put("id",id);
		List<?> list = hibernateDao.list(hql,params);
		model.addAttribute("bean",list.get(0));
		return "/admin/servant/detail";
	}

	@RequestMapping(value = "getOrganization", method = RequestMethod.POST)
	@ResponseBody
	public String getOrganization(HttpServletRequest request, HttpServletResponse response, Model model) {
		String hql = "from Organization";
		List<?> list = hibernateDao.list(hql);
		return JSON.toJSONString(list);
	}

	@RequestMapping(value = "listData", method = RequestMethod.POST)
	@ResponseBody
	public String listData(HttpServletRequest request, HttpServletResponse response, Model model) {
		String pageSize = request.getParameter("pageSize");
		String pageIndex = request.getParameter("pageIndex");
		String sortField = request.getParameter("sortField");
		String sortOrder = request.getParameter("sortOrder");

		String hql = "from Servant a where a.id=a.id ";
		Map<String, Object> params = new HashMap<String, Object>();

		String orgUid = request.getParameter("orgUid");
		if (!Functions.isEmpty(orgUid)) {
			Set<String> uidSet = new HashSet<>();
			String uid = orgUid;
			String hql2 = "from Organization where uid=:uid";
			Map<String, Object> params2 = new HashMap<String, Object>();
			params2.put("uid",uid);
			List<?> list2 = hibernateDao.list(hql2,params2);
			int st = 0;
			while(list2.size() > 0){
				Set<String> uidSet2 = new HashSet<>();
				for(int i=0;i<list2.size();i++){
					Organization org = (Organization) list2.get(i);
					uidSet.add(org.getUid());
					uidSet2.add(org.getUid());
				}
				//System.out.println("uidSet2:"+JSON.toJSONString(uidSet2));
				String hql3 = "from Organization where sid in(:uidArr)";
				Map<String, Object> params3 = new HashMap<String, Object>();
				params3.put("uidArr",uidSet2);
				list2 = hibernateDao.list(hql3,params3);
				st++;
				if(st>10){
					break;
				}
			}
			//System.out.println("uidSet:"+JSON.toJSONString(uidSet));

			hql += " and orgUid in (:orgUidArr)";
			params.put("orgUidArr", uidSet.toArray());
		}

		String username = request.getParameter("username");
		if (!Functions.isEmpty(username)) {
			hql += " and username like :username";
			params.put("username", "%" + username + "%");
		}

		Boolean sorted = !(sortField == null || "".equals(sortField) || sortOrder == null || "".equals(sortOrder));
		if (sorted) {
			hql += " order by :sortField :sortOrder";
			params.put("sortField", sortField);
			params.put("sortOrder", sortOrder);
		}else{
			hql += " order by id desc";
		}

		Boolean paged = !(pageSize == null || "".equals(pageSize) || pageIndex == null || "".equals(pageIndex));
		if (paged) {
			long total = hibernateDao.count(hql, params);
			List<?> list = hibernateDao.list(hql, params, Integer.parseInt(pageIndex) + 1, Integer.parseInt(pageSize));
			return JSON.toJSONString(new ListBean(list,total));
		} else {
			long total = hibernateDao.count(hql, params);
			List<?> list = hibernateDao.list(hql, params);
			return JSON.toJSONString(new ListBean(list,total));
		}
	}

}
